// 云函数入口文件
const cloud = require('wx-server-sdk')
var request = require('request')
cloud.init({
    env:"tfjkp-0ge80vu9ea710148"
})


// 云函数入口函数
exports.main = async (event, context) => {

    let jkpInfo = {"stuId":event.body.stuId,"stuName":event.body.stuName}

    if(event.type === "times"){
      return new Promise((resolve, reject) => {
        request({
          url: event.url,
          method: "POST",
          json: true,
          headers: {
            "content-type": "application/json",
        
          },
          body: jkpInfo
        }, function (error, response, body) {  
          console.log(body)         
          if (!error && response.statusCode == 200) {
            try {
              resolve(body)
            } catch (e) {
              reject()
            }
          }
        })
      })
    } else if(event.type === "listItem"){
      return new Promise((resolve, reject) => {
        request({
          url: event.url,
          method: "POST",
          json: true,
          headers: {
            "content-type": "application/json",
        
          },
          body: jkpInfo
        }, function (error, response, body) {  
          console.log(body)         
          if (!error && response.statusCode == 200) {
            try {
              resolve(body)
            } catch (e) {
              reject()
            }
          }
        })
      })
    }else if(event.type === "checkStu"){
      return new Promise((resolve, reject) => {
        request({
          url: event.url,
          method: "POST",
          json: true,
          headers: {
            "content-type": "application/json",
        
          },
          body: jkpInfo
        }, function (error, response, body) {  
          console.log(body)         
          if (!error && response.statusCode == 200) {
            try {
              resolve(body)
            } catch (e) {
              reject()
            }
          }
        })
      })
    }

}